package edu.zhuravlev.trainingbot.dispatcher;


import edu.zhuravlev.trainingbot.entity.User;
import edu.zhuravlev.trainingbot.service.SenderService;
import edu.zhuravlev.trainingbot.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.telegram.telegrambots.longpolling.util.LongPollingSingleThreadUpdateConsumer;
import org.telegram.telegrambots.meta.api.objects.Update;

import java.util.Objects;

@Component
@RequiredArgsConstructor
public class BotDispatcher implements LongPollingSingleThreadUpdateConsumer {

    private final SenderService senderService;

    private final UserService userService;

    @Override
    public void consume(Update update) {
        if (update.hasMessage() && update.getMessage().hasText()) {
            String message = update.getMessage().getText();

            if (Objects.equals(message, "/start")) {
                User user = userService.extractUser(update);

                userService.saveIfNotExists(user);
                senderService.sendSimpleText(update.getMessage().getChatId().toString(), "Бот тренировок приветствует %s".formatted(user.getName()));
            }
        }
    }

}
